package vn.vnvas.xoso.trungnam;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import vn.vnvas.xoso.LayoutItem;
import vn.vnvas.xoso.Mien;
import vn.vnvas.xoso.R;
import vn.vnvas.xoso.myAdapter;
import vn.vnvas.xoso.utils.WebService;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class mientrung extends Activity{
	
	private ListView mListView;
	private ArrayList<Mien> mArrayList;
	private myAdapter adapter;
	
	private JSONArray jArray;
	private JSONObject jObject;
	private WebService mWebService;
	
	private String loc_id,tentinh,jString;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mientrung);
		
		mListView = (ListView)findViewById(R.id.listViewMienTrung);
		mArrayList = new ArrayList<Mien>();
		adapter = new myAdapter(this, R.layout.layout_item, mArrayList);
		mListView.setAdapter(adapter);
		
		mWebService = new WebService();
		
		jString = mWebService.WsXoSo("TinhQuayThuong","mien","TRUNG");
		
		try {
			jArray = new JSONArray(jString);
			if(jArray.length()>0){
				for(int i =0;i<jArray.length();i++){
					jObject = jArray.getJSONObject(i);
					
					tentinh=jObject.getString("LocName");
					loc_id=jObject.getString("LocId");
					
					Mien tinh = new Mien(tentinh, BitmapFactory.decodeResource(getResources(), R.drawable.trung),loc_id);
					mArrayList.add(tinh);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int postition,
					long id) {
				String tinh_id = ((LayoutItem)view).loc_id.getText().toString();
				
				Intent intent1 = new Intent(getBaseContext(), KetQuaTheoTinh_TrungNam.class);
				intent1.putExtra("tinh_id", tinh_id);
				startActivity(intent1);
				
				
			}
		});
		
		
		
	}

}
